package com.seaboxdata.utils;

import com.robin.core.base.util.IOUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.ftp.FTPFileSystem;


/**
 * @author
 * @create 2021-02-05 10:10
 **/
public class HdfsTest {

    public static void main(String[] args) {
        String baseFilePath = "file:///D:/file";
        String basePath = "D:\\file";
        Configuration conf = new Configuration();
        FTPFileSystem fsLocal = new FTPFileSystem();
        Configuration localConf = new Configuration();
        localConf.set("fs.ftp.host", "172.21.112.14");
        localConf.set("fs.ftp.host.port", "21");
        localConf.set("fs.ftp.password.172.21.112.14", "123.com");
        localConf.set("fs.ftp.user.172.21.112.14", "zwdsj");

        FSDataInputStream in = null;
        try {
            fsLocal.initialize(URI.create("ftp//172.21.112.14:21"), localConf);
            RemoteIterator<LocatedFileStatus> files = fsLocal.listFiles(new Path("/20210406"), true);
            while (files.hasNext()) {
                LocatedFileStatus next = files.next();
                Path path = next.getPath();
                in = fsLocal.open(path);
                Configuration local = new Configuration();
                FileSystem fs = FileSystem.get(URI.create(baseFilePath), local);
                OutputStream out = fs.create(new Path(baseFilePath + path.toString()));
                IOUtils.copyBytes(in, out, 4096, true);
                unZip(basePath + path.toString(), basePath + path.toString().substring(0, path.toString().length() - 4),
                        "traffic@2019#");
                File f = new File(basePath + path.toString().substring(0, path.toString().length() - 4)
                        + "/dapeng_booking_stat_v.txt");
                BufferedReader reader = new BufferedReader(new FileReader(f));
                boolean flag = true;
                while (flag) {
                    reader.skip(1);
                    String value = reader.readLine();
                    if (value == null) {
                        flag = false;
                    } else {
                        System.out.println(value);
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void unZip(String source, String dest, String password) {
        try {
            File zipFile = new File(source);
            ZipFile zFile = new ZipFile(zipFile);
            zFile.setFileNameCharset("GBK");
            File destDir = new File(dest);
            if (!destDir.exists()) {
                destDir.mkdirs();
            }
            if (zFile.isEncrypted()) {
                zFile.setPassword(password.toCharArray());
            }
            zFile.extractAll(dest);
            List<FileHeader> headerList = zFile.getFileHeaders();
            List extractedFileList = new ArrayList<>();
            for (FileHeader fileHeader : headerList) {
                if (!fileHeader.isDirectory()) {
                    extractedFileList.add(new File(destDir, fileHeader.getFileName()));
                }
            }
            File[] extractedFiles = new File[extractedFileList.size()];
            extractedFileList.toArray(extractedFiles);
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }

}

